package servlet;

import Dao.MessageDao;
import eneity.Message;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String message = req.getParameter("message");
        String uName = (String)req.getSession().getAttribute("uName");
        String mDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Message message1 = new Message();
        message1.setMessage(message);
        message1.setUsername(uName);
        message1.setDate(mDate);
//		System.out.println("留言内容："+message);

        if(!message.equals("")) {
            try {
                new MessageDao().insert(message1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            resp.sendRedirect("MessageServlet");
        }else {
            //转发可能出现地址问题
            req.getSession().setAttribute("messageErrorMess", "您输入的留言内容为空！");
            resp.sendRedirect("message.jsp");
        }
    }
}
